package com.sigmazero13.MKDummy.activites;

import android.app.Dialog;
import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBar.Tab;
import android.support.v7.app.ActionBarActivity;

import com.sigmazero13.MKDummy.listeners.DummyPlayerTabListener;
import com.sigmazero13.MKDummy.models.ReferenceCard;

public class MKDummyPlayerActivity extends ActionBarActivity {
	
	private DummyPlayerTabListener<MKDummyPlayer> playerTabListener;
	private Tab playerTab;
	private Tab infoTab;
	private DummyPlayerTabListener<RefCardFragment> refCardTabListener;
		
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		ActionBar bar = getSupportActionBar();
		bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
		bar.setDisplayShowTitleEnabled(false);
		bar.setDisplayShowHomeEnabled(false);
		
		playerTabListener = new DummyPlayerTabListener<MKDummyPlayer>(this, "player", MKDummyPlayer.class);
		
		playerTab = bar.newTab().setText("Dummy").setTabListener(playerTabListener);
		bar.addTab(playerTab);
		
		Tab refTab = bar.newTab().setText("Ref. Cards").setTabListener(new DummyPlayerTabListener<RefCardListFragment>(this, "ref", RefCardListFragment.class));
		bar.addTab(refTab);
		
		refCardTabListener = new DummyPlayerTabListener<RefCardFragment>(this, "info", RefCardFragment.class);
		infoTab = bar.newTab().setText("No Card").setTabListener(refCardTabListener);
		bar.addTab(infoTab);
	}
	
	@Override
	public Dialog onCreateDialog(int id, Bundle args) {
		if (id != MKDummyPlayer.NEW_ROUND_DIALOG) return null;
		
		Dialog d = new NewRoundDialog(this, playerTabListener.getFragment());
		return d;
	}
	
	@Override
	public void onPrepareDialog(int id, Dialog d, Bundle args) {
		if (id != MKDummyPlayer.NEW_ROUND_DIALOG) return;
		
		((NewRoundDialog)d).prepare(args);
	}
	
	@Override
	public void onBackPressed() {
		ActionBar bar = getSupportActionBar();
		
		if (bar.getSelectedNavigationIndex() == 0) {
			playerTabListener.getFragment().setSaveStateOnDestroy(false);
			super.onBackPressed();
		} else {
			bar.setSelectedNavigationItem(0);
		}		
	}
	
	public void setPlayerTabName(String name) {
		playerTab.setText(name);
	}
	
	public void selectReferenceCard(ReferenceCard card) {
		ActionBar bar = getSupportActionBar();
		
		infoTab.setText(card.getTitle());
		refCardTabListener.getFragment().setReferenceCard(card);
		bar.setSelectedNavigationItem(2);
	}

}
